<template>
  <div class="baidu">
    <el-upload
      class="avatar-uploader"
      action="#"
      :class="{delpicture:fileList.length>0}"
      list-type="picture-card"
      :file-list="fileList"
      :on-preview="preview"
      :on-remove="remove"
      :on-change="onchange"
      :http-request="upload"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress
      v-if="showPercent"
      :percentage="percentage"
      style="width: 146px"
      :show-text="false"
    />
    <el-dialog title="预览" :visible="dialogVisible" @close="btnCancle">
      <el-row type="flex" justify="center">
        <img :src="imgUrl" alt="" @click="btnCancle">
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDoRnsgEWXGjvqTqyrSmrgEVWHlywf6G9n',
  SecretKey: 'PYP0JL6i5B0Yqsd6JHyOYS5RWLtAAtMY'
})
export default {
  data() {
    return {
      dialogVisible: false,
      fileList: [],

      imgUrl: '',
      percentage: 0,
      showPercent: false
    }
  },
  methods: {
    // handleAvatarSuccess(res, file) {
    //   this.imageUrl = URL.createObjectURL(file.raw)
    // },
    // beforeAvatarUpload(file) {
    //   const isJPG = file.type === 'image/jpeg'
    //   const isLt2M = file.size / 1024 / 1024 < 2

    //   if (!isJPG) {
    //     this.$message.error('上传头像图片只能是 JPG 格式!')
    //   }
    //   if (!isLt2M) {
    //     this.$message.error('上传头像图片大小不能超过 2MB!')
    //   }
    //   return isJPG && isLt2M
    // },
    preview(data) {
      this.imgUrl = data.url
      this.dialogVisible = true
    },
    btnCancle() {
      this.dialogVisible = false
    },
    remove(file, fileList) {
      this.fileList = [...fileList]
    },
    onchange(file, fileList) {
      this.fileList = [...fileList]
    },
    beforeUpload(file) {
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('只能上传jpeg,gif,png,bmp格式的图片')
        return false
      }
      const maxSize = 2 * 1024 * 1024
      if (maxSize < file.size) {
        this.$message.error('文件不能超过2M')
        return false
      }
      return true
    },
    upload(data) {
      this.showPercent = true
      cos.putObject({
        Bucket: '62renzi-1306249819', /* 必须 */
        Region: 'ap-nanjing', /* 存储桶所在地域，必须字段 */
        Key: data.file.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: (progressData) => {
          // console.log('进度条')
          // console.log(JSON.stringify(progressData))
          // console.log(progressData.percent)
          // console.log(this.percentage)
          this.percentage = progressData.percent * 100
          // console.log(this.percentage)
        }
      }, (err, data) => {
        console.log(err || data)
        if (!err) {
          // console.log('上传成功')
          // console.log(data.Location)
          // console.log(this.fileList[0].url)
          this.fileList[0].url = 'http://' + data.Location
          this.fileList[0].status = 'success'
          // this.$message.success('图片上传成功')

          setTimeout(() => {
            this.showPercent = false
          }, 1000)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.baidu{
  .delpicture {
  // /deep/ .el-upload--picture-card {
  // scss 不认识 /deep/ 要换成 ::v-deep
  ::v-deep .el-upload--picture-card {
    display: none;
  }
}
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px solid #eee;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .el-progress-bar__outer {
    top: -11px;
  }
}

</style>
